package sisi.cash;

import java.util.List;

import javax.persistence.EntityManager;

public class CashController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Cash[] get(String cerca) {
		List<Cash> lista = getList(cerca);
			return lista.toArray(new Cash[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Cash> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.invref) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Cash as c " + cWhere 
					+ " order by c.cashId ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Cash> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Cash> getList(String invref, String ref, String docType) {

		String cWhere = " WHERE TRIM( BOTH FROM UPPER(c.invref) ) = :invref AND TRIM( BOTH FROM UPPER(c.ref) ) = :ref AND c.doctype = :doctype ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Cash as c " + cWhere 
					+ " order by c.cashId ");

			q.setParameter("invref", invref.trim() );
			q.setParameter("ref", ref.trim() );
			q.setParameter("doctype", docType.trim() );

			List<Cash> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	
	public Cash getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Cash cashx = em.find(Cash.class, id);
			return cashx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Cash add(Cash cash) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(cash);
			em.getTransaction().commit();
			cash = refresh(cash);
		} finally {
			em.close();
			return cash;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Cash cash) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Cash cashx = em.find(Cash.class, cash.getCashId());
			cashx.setRef(cash.getRef());
			cashx.setNote(cash.getNote());
			cashx.setDoctype(cash.getDoctype());
			cashx.setCashId(cash.getCashId());
			cashx.setCalcAmount(cash.getCalcAmount());
			cashx.setCcDebitDt(cash.getCcDebitDt());
			cashx.setCcExpyredt(cash.getCcExpyredt());
			cashx.setCcNumber(cash.getCcNumber());
			cashx.setCcPayAmount(cash.getCcPayAmount());
			cashx.setCcPayDt(cash.getCcPayDt());
			cashx.setCcType(cash.getCcType());
			cashx.setChBank(cash.getChBank());
			cashx.setChPayAmount(cash.getChPayAmount());
			cashx.setChPayCurrDt(cash.getChPayCurrDt());
			cashx.setChPayDate(cash.getChPayDate());
			cashx.setChRef(cash.getChRef());
			cashx.setCheck(cash.getCheck());
			cashx.setCsAmount(cash.getCsAmount());
			cashx.setCsDt(cash.getCsDt());
			cashx.setCurr(cash.getCurr());
			cashx.setCustid(cash.getCustid());
			cashx.setDtUp(cash.getDtUp());
			cashx.setFiAmount(cash.getFiAmount());
			cashx.setFiExpyreDt(cash.getFiExpyreDt());
			cashx.setFiExpyred(cash.getFiExpyred());
			cashx.setFiRef(cash.getFiRef());
			cashx.setFiStartDt(cash.getFiStartDt());
			cashx.setFxRate(cash.getFxRate());
			cashx.setInvref(cash.getInvref());
			cashx.setPaymode(cash.getPaymode());
			cashx.setRbAmount(cash.getRbAmount());
			cashx.setRbDate(cash.getRbDate());
			cashx.setRbExpyreDt(cash.getRbExpyreDt());
			cashx.setSuppid(cash.getSuppid());
			cashx.setWtAmount(cash.getWtAmount());
			cashx.setWtDt(cash.getWtDt());
			cashx.setWtPayCurrDt(cash.getWtPayCurrDt());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Cash refresh(Cash cash) {

		Cash cashx = cash;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			cashx = em.find(Cash.class, cash.getCashId());
			em.flush();
			em.refresh(cashx);
		} finally {
			em.close();
			return cashx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Cash cash) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Cash cashx = em.find(Cash.class, cash.getCashId());
			em.remove(cashx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
